package com.mygdx.game.desktop.demo;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

/**
 * libGdx的table实现
 * @author Administrator
 *
 */
public class TableTestGame extends Game{

	Stage stage;
    Skin skin;

    Table menu;
    Table options;
    float x, y;
	@Override
	public void create() {
		stage = new Stage();
        //stage.setDebugAll(true);
        Gdx.input.setInputProcessor(stage);
        // 读取skin样式 uiskin.json来自官方
        skin = new Skin(Gdx.files.internal("skins/uiskin.json"));

        menu = new Table();

        options = new Table();
        stage.addActor(menu);
        stage.addActor(options);

        // Table的坐标是默认从屏幕左下角开始的， 调试中的蓝框就是Table
        // 如果设置下面属性则居中显示
        menu.setFillParent(true);
        TextButton tbStart = new TextButton("Start Game", skin);
        TextButton tbStart2 = new TextButton("Start Game2", skin);
        menu.add(tbStart); // 将开始按钮添加到menu Table中,方法返回cell
        menu.add(tbStart2).padLeft(50);// 此时2个并排显示 , 离左边的Button 50个像素
        menu.row();  // 在Table中新建一行
        TextButton helpTB = new TextButton("Help Game", skin);
        //menu.add(helpTB).expandX();  // 将帮助菜单的cell沿X轴展开
        menu.add(helpTB).colspan(2);   // 将2个Cell合并，并居中显示
        menu.row();
        // Space是单元格里的按钮和单元格之间的距离   Pad是单元格与单元格之间的距离
        TextButton testTB = new TextButton("Test Game", skin);
        menu.add(testTB);  // 这个bottom只是针对cell而言
        TextButton testTB2 = new TextButton("Test Game2", skin);
        menu.add(testTB2).space(40);
        menu.row();
        TextButton optionTB = new TextButton("Option", skin);
        optionTB.addListener(new ChangeListener() {
        	@Override
            public void changed(ChangeEvent event, Actor actor) {
                showOptionMenu(true);
            }
        });
		
        // menu.add(optionTB).padBottom(10); // 距离底部的Tabel 10个像素
        menu.add(optionTB).colspan(2).padTop(200);   // 距离上一个Cell的距离是200个像素

        // Table会根据你填充元素自动扩展
        Gdx.app.log("TAG", "menu table p w:" + menu.getPrefWidth() + " p h:" + menu.getPrefHeight());
        Gdx.app.log("TAG", "menu table w:" + menu.getWidth() + " h:" + menu.getHeight());

        TextButton soundTB = new TextButton("Sound", skin);
        options.add(soundTB);
        options.row();
        TextButton progressTB = new TextButton("Progress", skin);
        options.add(progressTB).padTop(20);
        options.row();
        TextButton  okTB = new TextButton("OK", skin);
        okTB.addListener(new ClickListener() {

            @Override
            public void clicked(InputEvent event, float x, float y) {
                showOptionMenu(false);
            }

        });
        options.add(okTB).padTop(40);
        options.setVisible(true);
        options.setSize(options.getPrefWidth(), options.getPrefHeight());
        x = stage.getWidth() - options.getWidth();
        y = stage.getHeight() / 2 - options.getHeight() / 2;
        options.setPosition(stage.getWidth() + 10f, y);
        options.setVisible(false);

	}
	
	@Override
    public void render() {
        Gdx.gl.glClearColor(0.39f, 0.58f, 0.92f, 1.0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act();
        stage.draw();
    }

	@Override
    public void dispose() {
        stage.dispose();
        skin.dispose();
    }

    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height);
    }

    /**
     * 控制侧栏的显隐
     * @param show
     */
    private void showOptionMenu(boolean show) {
        if (show) {
            menu.setVisible(!show);
            options.setVisible(show);
            options.addAction(Actions.moveTo(x, y, 0.3f, Interpolation.swingOut));
        } else {
            menu.setVisible(!show);
            options.setVisible(show);
            options.setPosition(stage.getWidth() + 10f, y);
        }
    }

    public static void main(String[] args) {
        new LwjglApplication(new TableTestGame());
    }
}